import logging
import time

class Logger:

    def log(self):

        # 创建一个日志器
        logger = logging.getLogger("logger")
        logger.setLevel(logging.INFO)

        if not logger.handlers:
            # 控制器
            fh = logging.FileHandler(filename=r"D:\pythoncode\trainning\log\{}".format(time.strftime("%Y-%m-%d %H-%M-%S")), encoding='utf-8')

            error_fh = logging.FileHandler(filename=r"D:\pythoncode\trainning\log\'error'+{}".format(time.strftime("%Y-%m-%d %H-%M-%S")), encoding='utf-8')
            fh.setLevel(level=logging.INFO)
            ch = logging.StreamHandler()
            ch.setLevel(logging.INFO)
            # 格式器
            Formator = logging.Formatter(fmt="%(asctime)s - %(filename)s - %(funcName)s - %(levelname)s - %(message)s",
                                         datefmt="%Y/%m/%d/%X")

            fh.setFormatter(Formator)
            error_fh.setLevel(level=logging.ERROR)
            error_fh.setFormatter(Formator)


            error_fh.setFormatter(Formator)

            logger.addHandler(fh)
            logger.addHandler(error_fh)
            logger.addHandler(ch)

        return logger
